package ten;

public class RRacer implements Runnable {
    private static String winner;

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            if (Thread.currentThread().getName().equals("兔子") && i % 10 == 0) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "--->" + i);
            if (gameOver(i)) {
                break;
            }
        }
    }

    private boolean gameOver(int step) {
        if (winner != null) {
            return true;
        } else {
            if (step == 100) {
                winner = Thread.currentThread().getName();
                System.out.println("winner: " + winner);
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        new Thread(new RRacer(), "乌龟").start();
        new Thread(new RRacer(), "兔子").start();
    }
}
